﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SLRT.business.src;

namespace SLRT.usercontrols
{
    /// <summary>
    /// Interaction logic for ButtonBar.xaml
    /// </summary>

    public partial class ButtonBarControl : UserControl
    {
        private int numberOfButtons = 4;
        private Button[] buttons;
        private WebcamControl parent;

        public ButtonBarControl(WebcamControl element)
        {
            InitializeComponent();
            this.parent = element;
            this.Name = "buttonBar";
            int i;
            
            buttons=new Button[numberOfButtons];
            double buttonSpace = parent.WebcamImage.ActualWidth / numberOfButtons;

            Style style = new Style(typeof(Button));
            style.Setters.Add(new Setter { Property = OverridesDefaultStyleProperty, Value = true });

            Setter templateSetter = new Setter();
            templateSetter.Property = TemplateProperty;

            ControlTemplate controlTemplate = new ControlTemplate(typeof(Button));

            FrameworkElementFactory fact = new FrameworkElementFactory(typeof(Border));

            controlTemplate.VisualTree = fact;

            fact.Name = "Border";
            //fact.SetValue(Border.BorderThicknessProperty, new Thickness(1));
            //fact.SetValue(Border.CornerRadiusProperty, new CornerRadius(2));
            fact.SetValue(Border.BackgroundProperty,new TemplateBindingExtension(Button.BackgroundProperty));
            //fact.SetValue(Border.BorderBrushProperty, Brushes.Aquamarine);

            Trigger triggerIsMouseOver = new Trigger { Property = Border.IsMouseOverProperty, Value = true };

            Binding b = new Binding();
            b.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent);
            b.Path = new PropertyPath(Button.BorderBrushProperty);

            Setter setter = new Setter(Border.BackgroundProperty, b, "Border");

            triggerIsMouseOver.Setters.Add(setter);

            controlTemplate.Triggers.Add(triggerIsMouseOver);
            templateSetter.Value = controlTemplate;

            style.Setters.Add(templateSetter);

            

            
            for (i = 0; i < numberOfButtons; i++)
            {
                buttons[i] = new Button();
                //buttons[i].Foreground = new ImageBrush(new BitmapImage(new Uri("test.png", UriKind.Relative)));
                buttons[i].Style = style;
                buttons[i].Background = new ImageBrush(new BitmapImage(new Uri((i+1)+"1.png", UriKind.Relative)));
                buttons[i].BorderBrush = new ImageBrush(new BitmapImage(new Uri((i+1)+"2.png", UriKind.Relative)));
                //buttons[i].Content = "Button" + i;
                buttons[i].Width = (buttonSpace-0.1*buttonSpace);
                buttons[i].Height = 100 - 20;
                buttons[i].Margin = new Thickness(0.05*buttonSpace, 10, 0.05*buttonSpace, 0);
                buttons[i].Click += new RoutedEventHandler(ButtonBar_Click);
                buttonStack.Children.Add(buttons[i]);
                
            }
        }

        void ButtonBar_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
        }

        public double[,] getButtonPositions()
        {
            double[,] pos = new double[numberOfButtons, 4];
            int i;

            for (i = 0; i < buttons.Length; i++)
            {
                pos[i, 0] = buttons[i].TranslatePoint(new Point(0, 0), buttonStack).X;
                pos[i, 1] = buttons[i].TranslatePoint(new Point(0, 0), buttonStack).Y;
                pos[i, 2] = buttons[i].Width + pos[i, 0];
                pos[i, 3] = buttons[i].Height + pos[i, 1];
            }

            return pos;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            CaptureModel.Instance.InitMovementBrain();
        }
    }
}
